package com.goktech.olala.client.controller.business;

import com.goktech.olala.core.req.ReqGoods;
import com.goktech.olala.core.resp.RespGoodsVo;
import com.goktech.olala.core.service.IQueryGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author sanming
 * @Description 商品搜索功能
 * @Date 2020/12/11 14:50
 */
@RestController
@RequestMapping("/goodsIndexApi")
public class QueryGoodController {

    @Autowired
    IQueryGoodService queryGoodService;

    @Autowired
    ReqGoods reqGoods;

    @RequestMapping("/queryGoodsByParam.do")
    public ModelAndView queryGoodsByParam(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        //获取用户输入关键词
        String keywords = request.getParameter("keywords");
        reqGoods.setKeywords(keywords);
        List<RespGoodsVo> respGoodsVos = null;
        try {
            respGoodsVos = queryGoodService.queryGoodsByParam(reqGoods);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mav.addObject("goodsList", respGoodsVos);

        //用于搜索栏搜索后的回显
        mav.addObject("keywords", keywords);

        //转到搜索页面
        mav.setViewName("home/search");

        return mav;
    }

    @RequestMapping("/queryGoodsDetail.do")
    public ModelAndView queryGoodsDetail(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        String goodsId = request.getParameter("goodsId");
        RespGoodsVo respGoodsVo=null;
        if (goodsId != null) {
            try {
                respGoodsVo = queryGoodService.selectByPrimaryKey(goodsId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mav.addObject("goodsInfo", respGoodsVo);

        mav.setViewName("home/introduction");

        return mav;
    }
}
